package com.zllkj.mm.common;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Environment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.zllkj.mm.MyApp;
import com.zllkj.mm.R;

import java.io.ByteArrayOutputStream;
import java.util.Map;
import java.util.regex.Pattern;

public class L {

    /**
     * constant
     */

    public static final String iM_userId = "iM_userId";
    public static final String token = "token";
    public static final String imToken = "imToken";
    public static final String info = "info";
    public static final String userInfo = "userInfo";
    public static final String history = "history";
    public static final String isRed = "isRed";//消息来的统一红点状态

    public static final String ifPerfectSelfInfo="ifPerfectSelfInfo";//是否完善个人信息：0-待完善，1-已完善
    public static final String IndexInfoS = "IndexInfoS";

    public static String ApkDownPath = Environment.getExternalStorageDirectory() + "/test/";
    public static String TestApkName = "test.apk";


    public static boolean mm = true;



    /**
     * set
     */

    private static long currentTime;
    private static long lateTime;


    public static void setText(View v, int id, String str) {
        ((TextView) v.findViewById(id)).setText(str);
    }

    public static void setImg(Object... urlId_view_id) {
        Object http = urlId_view_id[0] instanceof String ? (
                ((String) urlId_view_id[0]).startsWith("http") ||
                        ((String) urlId_view_id[0]).startsWith("/storage") ||
                        ((String) urlId_view_id[0]).startsWith("/data")
                        ? "" : ClientConfig.IMG_URL) + urlId_view_id[0]
                : (Integer) urlId_view_id[0];
        Glide.with(MyApp.getInstance())
                .load(http)
                .error(R.mipmap.img_null)
                .into(urlId_view_id[1] instanceof ImageView ? ((ImageView) urlId_view_id[1]) : (ImageView) ((View) urlId_view_id[1]).findViewById(((int) urlId_view_id[2])));
    }

    public static void setImgHead(Object... urlId_view_id) {
        Glide.with(MyApp.getInstance())
                .load(urlId_view_id[0] instanceof String ? (
                        ((String) urlId_view_id[0]).startsWith("http") ||
                                ((String) urlId_view_id[0]).startsWith("/storage") ||
                                ((String) urlId_view_id[0]).startsWith("/data")
                                ? "" : ClientConfig.IMG_URL) + urlId_view_id[0] : (Integer) urlId_view_id[0])
                .error(R.mipmap.ic_launcher)
                .into(urlId_view_id[1] instanceof ImageView ? ((ImageView) urlId_view_id[1]) : (ImageView) ((View) urlId_view_id[1]).findViewById(((int) urlId_view_id[2])));
    }

    public static void setImg_bitmap(Object... urlId_view_id) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ((Bitmap) urlId_view_id[0]).compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] bytes = baos.toByteArray();
        Glide.with(MyApp.getInstance())
                .load(bytes)
                .into(urlId_view_id[1] instanceof ImageView ? ((ImageView) urlId_view_id[1]) : (ImageView) ((View) urlId_view_id[1]).findViewById(((int) urlId_view_id[2]))
                );
    }


    /**
     * Toast
     */
    private static String lastToast = "";
    private static long lastToastTime;
    private static Toast toast;

    public static void showToast(String message) {
        if (message.equals("暂无数据")) {//暂无数据不再弹出
            return;
        }
        showToast(message, Toast.LENGTH_SHORT, 0, Gravity.CENTER);
    }

    public static void showToast(String message, int duration, int icon,
                                 int gravity) {
        if (message != null && !message.equalsIgnoreCase("")) {
            long time = System.currentTimeMillis();
//            if (!message.equalsIgnoreCase(lastToast) || Math.abs(time - lastToastTime) > 2000) {
            View view = LayoutInflater.from(MyApp.getInstance()).inflate(R.layout.util_toast, null);
            setText(view, R.id.tv_toast, message);
            if (icon != 0) {
                setImg(icon, view, R.id.iv_icon);
                view.findViewById(R.id.iv_icon).setVisibility(View.VISIBLE);
            }

            if (null == toast || Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {//9.0Toast特性
                if (null != toast) {
                    toast.cancel();
                }
                toast = new Toast(MyApp.getInstance());
            }

            toast.setView(view);
            if (gravity == Gravity.CENTER) {
                toast.setGravity(gravity, 0, 0);
            } else {
                toast.setGravity(gravity, 0, 35);
            }

            toast.setDuration(duration);
            toast.show();
            lastToast = message;
            lastToastTime = System.currentTimeMillis();
//            }
        }
    }


    /**
     * sign
     */
    public static Map sign(Map<String, Object> paramMap) {
        //TODO
        return paramMap;
    }

    public static boolean phoneRegex(String str) {
        return Pattern.matches("^((\\+?86)|(\\(\\+86\\)))?((((13[^4]{1})|(14[5-9]{1})|147|(15[^4]{1})|166|(17\\d{1})|(18\\d{1})|(19[89]{1}))\\d{8})|((134[^9]{1}|1410|1440)\\d{7}))$", str);
    }


}
